//
//  BaseInfoVC.m
//  MyProject
//
//  Created by jiaming yan on 2017/9/13.
//  Copyright © 2017年 yanjiaming. All rights reserved.
//

#import "BaseInfoVC.h"
#import "TimeSelectView.h"
#import "TypePickerView.h"
#import "IQTextView.h"
#import "SelectCityViewController.h"
#import "GetEducationListRootClass.h"
#import "YJMNavigationController.h"

@interface BaseInfoVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *sexTextField;
@property (weak, nonatomic) IBOutlet UITextField *birthdayTextField;
@property (weak, nonatomic) IBOutlet UITextField *qualificationsTextField;
@property (weak, nonatomic) IBOutlet UITextField *workYearTextField;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;

@property (copy, nonatomic) NSString *strMaxEduCode;
@property (copy, nonatomic) NSString *strCityId;
@property (copy, nonatomic) NSString *strBirthDay;
@property (strong, nonatomic) NSArray *arrEdu;
@property (strong, nonatomic) NSArray *arrEduCode;
@end

@implementation BaseInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"基本信息";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    [button setTitleColor:UIColorFromRGB(0x13bacd) forState:UIControlStateNormal];
    button.titleLabel.font = k15Font;
    button.frame = CGRectMake(0, 0, 40, 40);
    [button addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    
    negativeSpacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,barItem];
    if (IOS11Later) {
        button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
    [self setValues];

    [self sendRequest];
}

- (void)sendRequest{
    self.arrEdu = @[@"专科",@"本科",@"硕士",@"博士",@"博士以上"];
    self.arrEduCode = @[@"EDUCATION_ZK",@"EDUCATION_XS",@"EDUCATION_SX",@"EDUCATION_BS",@"EDUCATION_BSH"];
}

- (void)setValues{
    self.sexTextField.text = self.modelGetResume.sex;
    self.nameTextField.text = self.modelGetResume.name;
    self.qualificationsTextField.text = self.modelGetResume.maxEducationName;
    self.strMaxEduCode = self.modelGetResume.maxEducation;

    self.workYearTextField.text = [NSString stringWithFormat:@"%zd",self.modelGetResume.workYear];
    self.cityTextField.text = self.modelGetResume.regionName;
    self.strCityId = [NSString stringWithFormat:@"%zd",self.modelGetResume.region];
//    if (self.modelGetResume.birthYear!=0 && self.modelGetResume.birthMonth!=0) {
//        self.birthdayTextField.text = [NSString stringWithFormat:@"%ld.%ld出生",(long)self.modelGetResume.birthYear,(long)self.modelGetResume.birthMonth];
//    }
    self.birthdayTextField.text = self.modelGetResume.birthday;
}

-(void)saveButtonClick{
    
    NSString *strAlert = @"";
    
    if (self.nameTextField.text.length ==0 ) {
        strAlert = @"请输入姓名";
    }else if (self.sexTextField.text.length == 0){
        strAlert = @"请选择性别";
    }else if (self.strMaxEduCode.length == 0){
        strAlert = @"请选择最高学历";
    }else if (self.workYearTextField.text.length == 0){
        strAlert = @"请输入工作年限";
    }else if (self.strCityId.length == 0){
        strAlert = @"请选择所在城市";
    }
    
    if (strAlert.length != 0) {
        [CommonUtils showToast:strAlert];
        return;
    }
    
    NSLog(@"保存点击");
    NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                          self.nameTextField.text,@"name",
                                          self.strMaxEduCode,@"maxEducation",
                                          self.sexTextField.text,@"sex",
                                          self.workYearTextField.text,@"workYear",
                                          self.strCityId,@"region",
                                          self.birthdayTextField.text,@"birthday",
                                          nil];
    //简历id设置
    if (self.modelGetResume.resumeId != 0) {
        [dicInfoParams setObject:[NSString stringWithFormat:@"%zd",self.modelGetResume.resumeId] forKey:@"resumeId"];
    }
    

    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:dicInfoParams url:@"resume/updateResume" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    __weak typeof(self) weakSelf = self;
    if (textField == self.sexTextField) {
        [self.view endEditing:YES];
        TypePickerView *time = [[TypePickerView alloc]init];
        time.typeArr = @[@"男",@"女"];
        time.selectPicker = ^(NSString *selectStr){
            weakSelf.sexTextField.text = selectStr;
        };
        [time creatTypePickerView:self.view];
        return NO;
    }else if (textField == self.birthdayTextField) {
         [self.view endEditing:YES];
        TimeSelectView *time = [[TimeSelectView alloc]init];
        time.selectTime = ^(NSString *timeStr){
            weakSelf.birthdayTextField.text = timeStr;
            weakSelf.strBirthDay = timeStr;
        };
        [time creatTimeSelectView:self.view];
        [time.timePicker setDate:[NSDate dateWithTimeIntervalSince1970:1262275200]];
        time.timePicker.maximumDate= [NSDate date];
        return NO;
    }else if (textField == self.qualificationsTextField) {
         [self.view endEditing:YES];
        TypePickerView *time = [[TypePickerView alloc]init];
        time.typeArr = self.arrEdu;
        time.selectPicker = ^(NSString *eduStr){
            weakSelf.qualificationsTextField.text = eduStr;
            for (NSInteger i=0; i<weakSelf.arrEdu.count; i++) {
                 if ([eduStr isEqualToString:weakSelf.arrEdu[i]])
                     self.strMaxEduCode = weakSelf.arrEduCode[i];
            }
        };
        [time creatTypePickerView:self.view];
        return NO;
    }
   
    UIView *tempView = [self.view viewWithTag:909090901];
    [tempView removeFromSuperview];
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    UIView *tempView = [self.view viewWithTag:909090901];
    [tempView removeFromSuperview];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)cityClick:(id)sender {
    SelectCityViewController *scVC = [[SelectCityViewController alloc] init];
    YJMNavigationController *nav = [[YJMNavigationController alloc] initWithRootViewController:scVC];
    scVC.cityBlock = ^(CityModel *city) {
        self.strCityId = city.regionId;
        self.cityTextField.text = city.regionName;
    };
//    [self.navigationController pushViewController:scVC animated:YES];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)textViewDidChange:(UITextView *)textView{
    NSInteger number = [textView.text length];
    if (number > 100) {
        textView.text = [textView.text substringToIndex:100];
    }
 
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
